/**
 * 默认显示图片指令。在列表中试用这个这令，当没有数据的时候出现，显示没有数据
 * 或联网失败时候出现，提示联网失败，下拉刷新，或点击刷新
 */
APP.directive('defaultImage', ['$timeout', '$stateParams', 'AreaService', '$state',
  function ($timeout, $stateParams, AreaService, $state) {
    return {
      restrict: 'E',
      scope: {
        list: '=list',
        empty: '@empty',
        error: '@error'
      },
      template: '<div ng-if="isShow" class="text-align-c padding-top-30"><img class="width-60" ng-src="{{img}}" alt="默认图片"/><div class="color-text-m text-align-c margin-top-10" ng-bind="text"></div></div>',
      link: function (scope, element, attr) {
        scope.isShow = false;
        scope.text = '没有数据';

        scope.$watch('list.length', function (newVal, oldVal, scope) {
          //如果列表内容为空,显示'empty'图片以及 '没有数据'
          if (scope.list && scope.list.length == 0) {
            scope.img = scope.empty;
            scope.isShow = true;
            scope.text = '没有数据';
          } else if (scope.list && scope.list.length > 0) {
            scope.isShow = false;
          }
        });
        //如果网络错误,显示'error'图片以及 '网络错误，请下拉刷新'
        scope.$on('NETWORK:ERROR', function () {
          scope.img = scope.error;
          scope.text = '网络错误，请下拉刷新';
          scope.isShow = true;
        })
      }
    }
  }]);

